package com.ybkj.o2o.sales.controller.res;


import com.google.common.base.Preconditions;

import com.ybkj.o2o.sales.common.DataGrid;
import com.ybkj.o2o.sales.common.transport.Error;
import com.ybkj.o2o.sales.common.transport.Result;
import com.ybkj.o2o.sales.entity.ActivityManage;
import com.ybkj.o2o.sales.entity.ActivityManage.ServiceType;
import com.ybkj.o2o.sales.manager.ActivityManageManager;
import com.ybkj.o2o.sales.manager.ActivityManager;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;


@RestController
@RequestMapping(value = "api/activitymanage")
public class ActivityManageRes {

  @Autowired
  private ActivityManageManager activityManageManager;


  /**
   * 创建活动信息.
   * 
   * @param centreActivityImage 活动中心图片
   * @param businessActivityImage 业务弹窗图片图片
   * @param chainedAddress 链接地址
   * @param serviceType 业务类型
   * @param sort 排序
   * @param showLocation 展示位置
   * @param start 开始时间
   * @param end 结束时间
   * @param companyIds 公司id
   * @param companyName 公司名称
   * @param companyId 创建人公司id
   * @param appKey 系统key
   * @return ResultTransport{code,message,data}
   */
  @RequestMapping(value = "create", method = RequestMethod.POST)
  public ResponseEntity<Object> create(
      @RequestParam(required = false, value = "centreActivityImage") String centreActivityImage, 
      @RequestParam(required = false, value = "businessActivityImage")String businessActivityImage, 
      @RequestParam(required = false, value = "chainedAddress") String chainedAddress, 
      @RequestParam(required = false, value = "serviceType") String serviceType, 
      @RequestParam(required = false, value = "sort") Long sort,
      @RequestParam(required = false, value = "showLocation") String showLocation, 
      @RequestParam(required = true,  value = "start") Long start,
      @RequestParam(required = false, value = "end") Long end,
      @RequestParam(required = false, value = "companyIds") String companyIds, 
      @RequestParam(required = false, value = "companyName") String companyName, 
      @RequestParam(required = false, value = "companyId") Long companyId, 
      @RequestParam(required = true,  value = "appKey") String appKey) {

    ResponseEntity<Object> responseEntity = null;

    try {
      ActivityManage activityManage = new ActivityManage();
      activityManage.setCentreActivityImage(centreActivityImage);
      activityManage.setBusinessActivityImage(businessActivityImage);
      if (StringUtils.isNotBlank(chainedAddress)) {
        activityManage.setChainedAddress(chainedAddress);

      }
      activityManage.setServiceType(ServiceType.valueOf(serviceType));
      activityManage.setSort(sort);
      activityManage.setShowLocation(showLocation);
      activityManage.setStart(new Date(start));
      if (null != end) {
        activityManage.setEnd(new Date(end));
      }
      if (StringUtils.isBlank(companyIds) && companyId != null) {
        companyIds = "," + companyId + ",";
      }
      activityManage.setCompanyIds(companyIds);
      activityManage.setCompanyName(companyName);
      activityManage.setCompanyId(companyId);

      activityManage.setAppKey(appKey);
      activityManage = activityManageManager.insert(activityManage);

      Result<ActivityManage> result = new Result<ActivityManage>(0, "成功", activityManage);
      responseEntity = new ResponseEntity<Object>(result, HttpStatus.OK);

      return responseEntity;

    } catch (Exception ex) {
      ex.printStackTrace();
      Error error = new Error(500, "内部服务器错误：" + ex.getMessage());

      responseEntity = new ResponseEntity<Object>(error, HttpStatus.INTERNAL_SERVER_ERROR);

      return responseEntity;
    }

  }


  /**
   * 分页查询活动信息.
   * 
   * @param companyIds 公司id
   * @param chainedAddress 链接地址
   * @param serviceType 业务类型
   * @param sort 排序
   * @param showLocation 展示位置
   * @param page 页数
   * @param rows 每页数量
   * @param appKey 系统key
   * @return ResultTransport{code,message,data}
   */
  @RequestMapping(value = "queryActivityManage", method = RequestMethod.GET)
  public ResponseEntity<Object> queryActivities(@RequestParam(required = false,
      value = "companyIds") String companyIds, @RequestParam(required = false,
      value = "chainedAddress") String chainedAddress, @RequestParam(required = false,
      value = "serviceType") String serviceType,
      @RequestParam(required = false, value = "sort") Long sort, @RequestParam(required = false,
          value = "showLocation") String showLocation, @RequestParam(required = false,
          value = "page") Integer page,
      @RequestParam(required = false, value = "rows") Integer rows, @RequestParam(required = true,
          value = "appKey") String appKey) {

    ResponseEntity<Object> responseEntity = null;
    String companyid = null;

    if (companyIds != null) {
      companyid = "%," + companyIds + ",%";
    }

    try {
      DataGrid<ActivityManage> activityManage =
          activityManageManager.queryActivityManage(companyid, chainedAddress, serviceType, sort,
              showLocation, page, rows, appKey);
      Result<DataGrid<ActivityManage>> result =
          new Result<DataGrid<ActivityManage>>(0, "成功", activityManage);
      responseEntity = new ResponseEntity<Object>(result, HttpStatus.OK);

      return responseEntity;

    } catch (Exception ex) {
      ex.printStackTrace();
      Error error = new Error(500, "内部服务器错误：" + ex.getMessage());

      responseEntity = new ResponseEntity<Object>(error, HttpStatus.INTERNAL_SERVER_ERROR);

      return responseEntity;
    }

  }

  /**
   * 根据公司Id、业务类型分页查询活动信息.
   * 
   * @param companyId 公司id
   * @param serviceType 业务类型
   * @param page 页数
   * @param rows 每页数量
   * @param appKey 系统key
   * @return ResultTransport{code,message,data}
   */
  @RequestMapping(value = "queryByCompanyId", method = RequestMethod.GET)
  public ResponseEntity<Object> queryByCompanyId(
      @RequestParam(required = true, value = "companyId") String companyId, @RequestParam(
          required = false, value = "serviceType") String serviceType, @RequestParam(
          required = false, value = "page") Integer page, @RequestParam(required = false,
          value = "rows") Integer rows,
      @RequestParam(required = true, value = "appKey") String appKey) {

    ResponseEntity<Object> responseEntity = null;
    String companyid = null;

    if (companyId != null) {
      companyid = "%," + companyId + ",%";
    }
    try {
      DataGrid<ActivityManage> activityManage =
          activityManageManager.queryByCompanyId(companyid, serviceType, page, rows, appKey);
      Result<DataGrid<ActivityManage>> result =
          new Result<DataGrid<ActivityManage>>(0, "成功", activityManage);
      responseEntity = new ResponseEntity<Object>(result, HttpStatus.OK);

      return responseEntity;

    } catch (Exception ex) {
      ex.printStackTrace();
      Error error = new Error(500, "内部服务器错误：" + ex.getMessage());

      responseEntity = new ResponseEntity<Object>(error, HttpStatus.INTERNAL_SERVER_ERROR);

      return responseEntity;
    }

  }


  /**
   * 根据ID查询单个活动信息.
   * 
   * @param activityManageId 活动信息id
   * @param appKey 系统key
   * @return ResultTransport{code,message,data}
   */
  @RequestMapping(value = "{activityManageId}", method = RequestMethod.GET)
  public ResponseEntity<Object> getActivity(@PathVariable("activityManageId")Long activityManageId,
                                            @RequestParam("appKey") String appKey) {

    ResponseEntity<Object> responseEntity = null;

    appKey = StringUtils.trimToEmpty(appKey);

    ActivityManage activityManage = activityManageManager.findOne(activityManageId, appKey);
    Preconditions.checkArgument(activityManage != null, "活动不存在");

    try {

      Result<ActivityManage> result = new Result<ActivityManage>(0, "成功", activityManage);

      responseEntity = new ResponseEntity<Object>(result, HttpStatus.OK);

      return responseEntity;

    } catch (Exception ex) {

      Error error = new Error(500, "内部服务器错误：" + ex.getMessage());

      responseEntity = new ResponseEntity<Object>(error, HttpStatus.INTERNAL_SERVER_ERROR);

      return responseEntity;
    }
  }


  /**
   * 删除.
   * 
   * @param id 活动信息id
   * @param appKey 系统key
   */
  @RequestMapping(value = "delete", method = RequestMethod.DELETE)
  public ResponseEntity<Object> delete(@RequestParam(required = false, value = "id") Long id,
      @RequestParam(required = true, value = "appKey") String appKey) {

    ResponseEntity<Object> responseEntity = null;

    try {
      activityManageManager.delete(id, appKey);
      System.out.println("ActivityManageRes.delete()");
      Result<ActivityManager> result = new Result<ActivityManager>(0, "成功", null);
      responseEntity = new ResponseEntity<Object>(result, HttpStatus.OK);

      return responseEntity;

    } catch (Exception ex) {
      ex.printStackTrace();
      Error error = new Error(500, "内部服务器错误：" + ex.getMessage());

      responseEntity = new ResponseEntity<Object>(error, HttpStatus.INTERNAL_SERVER_ERROR);

      return responseEntity;
    }

  }

  /**
   * 修改活动信息.
   * 
   * @param id 活动信息Id
   * @param centreActivityImage 活动中心图片
   * @param businessActivityImage 业务弹窗图片图片
   * @param chainedAddress 链接地址
   * @param serviceType 业务类型
   * @param sort 排序
   * @param showLocation 展示位置
   * @param end 结束时间
   * @param companyIds 公司id
   * @param companyName 公司名称
   * @param appKey 系统key
   * @return ResultTransport{code,message,data}
   */
  @RequestMapping(value = "update", method = RequestMethod.POST)
  public ResponseEntity<Object> update(
      @RequestParam(required = true, value = "id") Long id,
      @RequestParam(required = false, value = "centreActivityImage") String centreActivityImage,
      @RequestParam(required = false, value = "businessActivityImage") String businessActivityImage,
      @RequestParam(required = false, value = "chainedAddress") String chainedAddress,
      @RequestParam(required = true, value = "serviceType") String serviceType, @RequestParam(
          required = true, value = "sort") Long sort, @RequestParam(required = true,
          value = "showLocation") String showLocation, @RequestParam(required = false,
          value = "end") Long end,
      @RequestParam(required = false, value = "companyIds") String companyIds, @RequestParam(
          required = false, value = "companyName") String companyName, @RequestParam(
          required = true, value = "appKey") String appKey) {
    ResponseEntity<Object> responseEntity = null;
    appKey = StringUtils.trimToEmpty(appKey);
    try {
      ActivityManage activityManage = activityManageManager.findOne(id, appKey);
      if (activityManage != null) {
        activityManage.setCentreActivityImage(centreActivityImage);
        activityManage.setBusinessActivityImage(businessActivityImage);
        if (StringUtils.isNotBlank(chainedAddress)) {
          activityManage.setChainedAddress(chainedAddress);
        }
        activityManage.setServiceType(ServiceType.valueOf(serviceType));
        activityManage.setSort(sort);
        activityManage.setShowLocation(showLocation);
        if (null != end) {
          activityManage.setEnd(new Date(end));
        }
        if (StringUtils.isNotBlank(companyIds)) {
          activityManage.setCompanyIds(companyIds);
        }
        activityManage.setCompanyName(companyName);
        activityManageManager.update(activityManage);

      }

      System.out.println("ActivityManageRes.update()");
      Result<ActivityManage> result = new Result<ActivityManage>(0, "成功", activityManage);

      responseEntity = new ResponseEntity<Object>(result, HttpStatus.CREATED);

      return responseEntity;
    } catch (Exception ex) {
      ex.printStackTrace();
      Error error = new Error(500, "内部服务器错误：" + ex.getMessage());
      responseEntity = new ResponseEntity<Object>(error, HttpStatus.INTERNAL_SERVER_ERROR);
      return responseEntity;
    }

  }


}
